package game.web.dao.impl;

import game.web.dao.MyBankDAO;
import game.web.dao.dto.MyBankDTO;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import projectx.core.base.ProjectXSystemException;
import projectx.core.dao.AbstractDAO;
import projectx.core.dao.DataAccessException;
import projectx.core.dao.ExceptionHandler;
import projectx.core.dao.Parameter;
import projectx.core.dao.impl.PreparedStatementBuilder;

public class MyBankDAOImpl extends AbstractDAO implements MyBankDAO {

	public enum field {
		id,
		bankinfo,
		banktype,
		bankno,
		zhifubao,
		caifutong,
		createtime,
		updatetime,
		createip,
		updateip
	}

	public MyBankDAOImpl() throws ProjectXSystemException {
		super();
	}

	@Override
	public MyBankDTO select(String userid) throws DataAccessException {
		MyBankDTO dto = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"MyBankDAO_SELECT");
			List paramKeys = new ArrayList();
			Parameter param = new Parameter();
			param.setString("id", userid);
			
			sql = _builder.getSql(param, paramKeys);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, param, paramKeys);
		    
			System.err.println("BankDao-->sql="+sql);
			debugSql();

			rs = ps.executeQuery();

			if (rs.next()) {
				String id = getString(rs.getString(field.id.ordinal()+1));
				String bankinfo = getString(rs.getString(field.bankinfo.ordinal()+1));
				int banktype = (rs.getInt(field.banktype.ordinal()+1));
				String bankno = getString(rs.getString(field.bankno.ordinal()+1));
				String zhifubao = getString(rs.getString(field.zhifubao.ordinal()+1));
				String caifutong = getString(rs.getString(field.caifutong.ordinal()+1));
				String createtime = (rs.getString(field.createtime.ordinal()+1));
				String updatetime = (rs.getString(field.updatetime.ordinal()+1));
				String createip = getString(rs.getString(field.createip.ordinal()+1));
				String updateip = getString(rs.getString(field.updateip.ordinal()+1));
				
				dto = new MyBankDTO();
				dto.setId(id);
				dto.setBankinfo(bankinfo);
				dto.setBanktype(banktype);
				dto.setBankno(bankno);
				dto.setZhifubao(zhifubao);
				dto.setCaifutong(caifutong);
				dto.setCreatetime(createtime);
				dto.setUpdatetime(updatetime);
				dto.setCreateip(createip);
				dto.setUpdateip(updateip);
				count++;
			}

			if (count == 1 && rs.next()) {
				count++;
			}
		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (count > 1) {
			_exHandler.handleQueryException(null, ExceptionHandler.MULTIFOUND);
		}
		return dto;
	}
	@Override
	public boolean insert(MyBankDTO mybankDTO) throws DataAccessException {
		MyBankDTO dto = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"MyBankDAO_INSERT");
			List paramKeys = new ArrayList();
			Parameter param = new Parameter();
			param.setString("id", mybankDTO.getId());
			param.setString("bankinfo", mybankDTO.getBankinfo());
			param.setInt("banktype", mybankDTO.getBanktype());
			param.setString("bankno", mybankDTO.getBankno());
			param.setString("zhifubao", mybankDTO.getZhifubao());
			param.setString("caifutong", mybankDTO.getCaifutong());
			param.setString("createtime", mybankDTO.getCreatetime());
			param.setString("updatetime", mybankDTO.getUpdatetime());
			param.setString("createip", mybankDTO.getCreateip());
			param.setString("updateip", mybankDTO.getUpdateip());
			
			sql = _builder.getSql(param, paramKeys);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, param, paramKeys);
			
			debugSql();

			return ps.execute();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (count > 1) {
			_exHandler.handleQueryException(null, ExceptionHandler.MULTIFOUND);
		}
		return false;
	}
	
	@Override
	public boolean update(MyBankDTO dto) throws DataAccessException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String sql = null;
		try {
			_builder = (PreparedStatementBuilder) SqlLoader.getBuilders().get(
					"MyBankDAO_UPDATE");
			List paramKeys = new ArrayList();
			Parameter param = new Parameter();
			param.setString("id", dto.getId());
			param.setString("bankinfo", dto.getBankinfo());
			param.setInt("banktype", dto.getBanktype());
			param.setString("bankno", dto.getBankno());
			param.setString("zhifubao", dto.getZhifubao());
			param.setString("caifutong", dto.getCaifutong());
			param.setString("createtime", dto.getCreatetime());
			param.setString("updatetime", dto.getUpdatetime());
			param.setString("createip", dto.getCreateip());
			param.setString("updateip", dto.getUpdateip());
			
			sql = _builder.getSql(param, paramKeys);
			ps = _builder.createStatement(_conn, sql, false, false);
			_builder.setParameter(ps, param, paramKeys);
			
			debugSql();

			return ps.execute();

		} catch (Exception e) {
			errorSql(e);
			_exHandler.handleQueryException(e, ExceptionHandler.UNKNOWN);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				logError(e);
			}
		}
		if (count > 1) {
			_exHandler.handleQueryException(null, ExceptionHandler.MULTIFOUND);
		}
		return false;
	}
}
